#include "stm32f10x.h"                  // Device header
#include "OLED.H"
#include <stdio.h>
#include "Delay.h"
#include "MyUSART.H"
#include "esp.h"
#include "DHT11.H"
#include "MySPI.H"
#include "Timer.H"
#include "PWM.H"
#include "RC522.h"
#define INTERNET_CONNECTED 0

int Temperature;
int Shidu;
uint8_t Lock=0x00;
uint8_t Switch2=0;
extern char RECS[200];
extern char Data[5];
extern const char* pubtopic3;

uint32_t cnt;

uint8_t cardnumber,KeyNum,tempcard,select=0,flag_scan=1,flag_addcard=0,flag_deletecard=0;

uint8_t open_lock;

extern uint8_t UID[4],Temp[4];
extern uint8_t UI0[4];							//卡片0ID数组
extern uint8_t UI1[4];							//卡片1ID数组
extern uint8_t UI2[4];							//卡片2ID数组
extern uint8_t UI3[4];							//卡片3ID数组

void RFID_Check(void);


void Init(void)
{
	OLED_Init();
	OLED_ShowString(1,1,"Linking...");
	MyUSART_Init(); //初始化串口
	//Timer_Init();
	PWM_Init();
	
	#if INTERNET_CONNECTED
	uint8_t Judge=0;
	do
	{
		Judge = esp_Init();
		OLED_ShowString(1,1,"error code:    ");
		OLED_ShowNum(2,1,Judge,1);
	}while(Judge);  //连接阿里云直到成功
	#endif
	
	RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA ,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	GPIO_ResetBits(GPIOA,GPIO_Pin_2);//LED初始化为低电平（关）
	
	//rc522 init
	RFID_Init();
	
}
uint8_t debug_error_card_flag = 0;
int main(void)
{
	Init();
	
	while(1)
	{	
		OLED_ShowString(3,1,"Lock");
		OLED_ShowString(1,1,"Tempera  Humidi");
		
		Delay_ms(10);
		cnt++;
		if(cnt%100==0)
			DHT11_GetData(); //约每1s执行一次温湿度采集
		#if INTERNET_CONNECTED
		if(cnt==150) //约每6s执行一次数据上报
		{
			if(Esp_PUB() == 1)
			{
				OLED_ShowString(1,1,"publish failed");
				Delay_ms(500);
				OLED_Clear();
			}
			cnt=0;
		}
		#endif
		
		Shidu=Data[0];
		Temperature=Data[2];
		if(debug_error_card_flag){
			OLED_SetCursor(2, 0);
			for(uint8_t i = 0; i < 128; i++)
			{
				OLED_WriteData(0x00);
			}
			OLED_SetCursor(3, 0);
			for(uint8_t i = 0; i < 128; i++)
			{
				OLED_WriteData(0x00);
			}
		}
		
		/***debug!!***/
		
		OLED_ShowNum(2,2,Temperature,2);OLED_ShowNum(2,13,Shidu,2);
		OLED_ShowChar(4,2,Lock);
		//OLED_ShowChar(4,13,Switch2);
		
		//检测刷卡
		RFID_Check();
		
		
		if(Lock=='0')
			Servo_SetAngle(0);	
		else if(Lock=='1')
			Servo_SetAngle(90);	
		if(Switch2=='0')
			GPIO_ResetBits(GPIOA,GPIO_Pin_2);
		else if(Switch2=='1')
			GPIO_SetBits(GPIOA,GPIO_Pin_2);
		
		Delay_ms(10);
	}
	
}

//读卡函数，读卡并获取卡编号
void RFID_Check()									
{
	cardnumber = Rc522Test();	//获取卡编号
	if(cardnumber == 0)			//如果为0，表示“卡片错误”，系统中没有这张卡
	{
		OLED_ShowString(1,1,"   Error card   ");
		/***debug!!***/
		debug_error_card_flag = 1;
		/***debug!!***/
		
//		Buzzer_Alarm();		//蜂鸣器发出警报
		OLED_ShowHexNum(2,1,UID[0],2);
		OLED_ShowHexNum(2,3,UID[1],2);
		OLED_ShowHexNum(2,5,UID[2],2);
		OLED_ShowHexNum(2,7,UID[3],2);
		WaitCardOff();		//等待卡片移开
	}
	else if(cardnumber==1||cardnumber==2||cardnumber==3||cardnumber == 4)			//如果卡编号为1-4，说明是系统中的4张卡
	{	
		OLED_ShowString(1,1,"The CardID is:  ");
		OLED_ShowNum(1,15,cardnumber,2);
		
		/***debug!!***/
		Lock = '1';
		OLED_ShowChar(4,2,Lock);
		/***debug!!***/
		
//		Buzzer2();			//蜂鸣器响两声
		Servo_SetAngle(90);	//舵机旋转90度维持1.5秒
		printf("AT+MQTTPUB=0,\"%s\",\"%d\",0,0\r\n",pubtopic3,1);
		WaitCardOff();		//等待卡片移开
		Delay_ms(1500);	//维持1秒
		Servo_SetAngle(0);	//舵机旋转90度
		printf("AT+MQTTPUB=0,\"%s\",\"%d\",0,0\r\n",pubtopic3,0);
		
		/***debug!!***/
		Lock = '0';
		OLED_ShowChar(4,2,Lock);
		/***debug!!***/
	}	
}





